package com.yijiaw.dao;

import com.yijiaw.domain.Message;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.skyway.spring.util.dao.AbstractJpaDao;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

/**
 * DAO to manage Message entities.
 * 
 */
@Repository("MessageDAO")
@Transactional
public class MessageDAOImpl extends AbstractJpaDao<Message> implements
		MessageDAO {

	/**
	 * Set of entity classes managed by this DAO.  Typically a DAO manages a single entity.
	 *
	 */
	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(Arrays.asList(new Class<?>[] { Message.class }));

	/**
	 * EntityManager injected by Spring for persistence unit yijiaw
	 *
	 */
	@PersistenceContext(unitName = "yijiaw")
	private EntityManager entityManager;

	/**
	 * Instantiates a new MessageDAOImpl
	 *
	 */
	public MessageDAOImpl() {
		super();
	}

	/**
	 * Get the entity manager that manages persistence unit 
	 *
	 */
	public EntityManager getEntityManager() {
		return entityManager;
	}

	/**
	 * Returns the set of entity classes managed by this DAO.
	 *
	 */
	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	/**
	 * JPQL Query - findMessageByContent
	 *
	 */
	@Transactional
	public Set<Message> findMessageByContent(String content) throws DataAccessException {

		return findMessageByContent(content, -1, -1);
	}

	/**
	 * JPQL Query - findMessageByContent
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Message> findMessageByContent(String content, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findMessageByContent", startResult, maxRows, content);
		return new LinkedHashSet<Message>(query.getResultList());
	}

	/**
	 * JPQL Query - findMessageByCompanyContaining
	 *
	 */
	@Transactional
	public Set<Message> findMessageByCompanyContaining(String company) throws DataAccessException {

		return findMessageByCompanyContaining(company, -1, -1);
	}

	/**
	 * JPQL Query - findMessageByCompanyContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Message> findMessageByCompanyContaining(String company, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findMessageByCompanyContaining", startResult, maxRows, company);
		return new LinkedHashSet<Message>(query.getResultList());
	}

	/**
	 * JPQL Query - findMessageByDate
	 *
	 */
	@Transactional
	public Set<Message> findMessageByDate(java.util.Calendar date) throws DataAccessException {

		return findMessageByDate(date, -1, -1);
	}

	/**
	 * JPQL Query - findMessageByDate
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Message> findMessageByDate(java.util.Calendar date, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findMessageByDate", startResult, maxRows, date);
		return new LinkedHashSet<Message>(query.getResultList());
	}

	/**
	 * JPQL Query - findAllMessages
	 *
	 */
	@Transactional
	public Set<Message> findAllMessages() throws DataAccessException {

		return findAllMessages(-1, -1);
	}

	/**
	 * JPQL Query - findAllMessages
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Message> findAllMessages(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllMessages", startResult, maxRows);
		return new LinkedHashSet<Message>(query.getResultList());
	}

	/**
	 * JPQL Query - findMessageById
	 *
	 */
	@Transactional
	public Message findMessageById(Integer id) throws DataAccessException {

		return findMessageById(id, -1, -1);
	}

	/**
	 * JPQL Query - findMessageById
	 *
	 */

	@Transactional
	public Message findMessageById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findMessageById", startResult, maxRows, id);
			return (com.yijiaw.domain.Message) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * JPQL Query - findMessageByPhoneContaining
	 *
	 */
	@Transactional
	public Set<Message> findMessageByPhoneContaining(String phone) throws DataAccessException {

		return findMessageByPhoneContaining(phone, -1, -1);
	}

	/**
	 * JPQL Query - findMessageByPhoneContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Message> findMessageByPhoneContaining(String phone, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findMessageByPhoneContaining", startResult, maxRows, phone);
		return new LinkedHashSet<Message>(query.getResultList());
	}

	/**
	 * JPQL Query - findMessageByName
	 *
	 */
	@Transactional
	public Set<Message> findMessageByName(String name) throws DataAccessException {

		return findMessageByName(name, -1, -1);
	}

	/**
	 * JPQL Query - findMessageByName
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Message> findMessageByName(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findMessageByName", startResult, maxRows, name);
		return new LinkedHashSet<Message>(query.getResultList());
	}

	/**
	 * JPQL Query - findMessageByNameContaining
	 *
	 */
	@Transactional
	public Set<Message> findMessageByNameContaining(String name) throws DataAccessException {

		return findMessageByNameContaining(name, -1, -1);
	}

	/**
	 * JPQL Query - findMessageByNameContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Message> findMessageByNameContaining(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findMessageByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Message>(query.getResultList());
	}

	/**
	 * JPQL Query - findMessageByPrimaryKey
	 *
	 */
	@Transactional
	public Message findMessageByPrimaryKey(Integer id) throws DataAccessException {

		return findMessageByPrimaryKey(id, -1, -1);
	}

	/**
	 * JPQL Query - findMessageByPrimaryKey
	 *
	 */

	@Transactional
	public Message findMessageByPrimaryKey(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findMessageByPrimaryKey", startResult, maxRows, id);
			return (com.yijiaw.domain.Message) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * JPQL Query - findMessageByPhone
	 *
	 */
	@Transactional
	public Set<Message> findMessageByPhone(String phone) throws DataAccessException {

		return findMessageByPhone(phone, -1, -1);
	}

	/**
	 * JPQL Query - findMessageByPhone
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Message> findMessageByPhone(String phone, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findMessageByPhone", startResult, maxRows, phone);
		return new LinkedHashSet<Message>(query.getResultList());
	}

	/**
	 * JPQL Query - findMessageByCompany
	 *
	 */
	@Transactional
	public Set<Message> findMessageByCompany(String company) throws DataAccessException {

		return findMessageByCompany(company, -1, -1);
	}

	/**
	 * JPQL Query - findMessageByCompany
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Message> findMessageByCompany(String company, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findMessageByCompany", startResult, maxRows, company);
		return new LinkedHashSet<Message>(query.getResultList());
	}

	/**
	 * Used to determine whether or not to merge the entity or persist the entity when calling Store
	 * @see store
	 * 
	 *
	 */
	public boolean canBeMerged(Message entity) {
		return true;
	}
}
